<?php
session_start();
cls_General_Forms::code_block_starts('');

//echo 'this is the content of the center';
 #########################################################################  
$blocked = security::head_entry();
  if($blocked){      
  #Form to be loaded in case no post or request has been made		
    if((!isset($_POST['submit']) || !isset($_REQUEST)) && !isset($_SESSION['username'])){
       cls_General_Forms::login_form();  
        #get all the users for display
        
                      // $user_type=admin_functions::get_users();
                       #display the adding form
                      // admin_forms::add_user($user_type);
                     
        }else{
           // echo "<center style='font-size:24pt';>Welcome to SSC Student Information System</center>";
        }
  }else{
      die("<p class='warnings'>This address is temporarily closed due to successive failed login attempts</p>");
  }
#this will be the processing part of posts and requests
if(isset($_POST['submit'])){
    switch($_POST['submit']){

         case 'Print COG':
                #new student to enroll, it gets the grades to generate a draft copy of COG
               $student=new general_student($_POST['student_id']);
                    if($student->check_valid_user()){
                        $grades  = $student->get_cog($_POST['yr_lvl'],$_POST['semester']);
                        $profile = $student->get_profile();
                                               
                        #check if  he has paid already the request 
                         $status_payment = admin_functions::check_if_paid("Certification Fee",$_POST);
                         if($status_payment['count']){
                             #display the registratio form
                              registar_forms::draft_cog_unprint($grades,$profile,$_POST['yr_lvl'],$_POST['semester']);
                         }else{
                            die('You have not yet paid your certification of grades assessment.');   
                         }
                       
                    }else{
                        die("<p class='warnings'>you are not a valid user</p>");
                        }
                unset($student);
            break;
         case 'Request Evaluation':   
         case 'Request COG':
            #this function will insert a payable to the student ledger;
                admin_functions::update_ledger_collectibles_cog($_POST);
            break;   
            
            case 'LOGIN':
                    #this will check validity of credentials if  true
                    $profile=admin_functions::check_user_profile($_POST['username'],$_POST['password'],$_POST['profile_id']);  
                        if($profile){                               
                              #this will reset the counter of the ip
                              $_SESSION['page']="Home";  
                              header('location:main.php');
                        }else{
                            die("<p class='warnings'>Invalid user credentials!</p>");
                        }           
                            
                
                break;       
                
            case 'PRINT EVALUATION':
                $student=new general_student($_POST['student_id']);
                  if($student->check_valid_user()){
                    #check if payment is made;returns array of options(count,yr_lvl, semester)
                    $status_payment=admin_functions::check_if_paid("Evaluation Fee",$_POST);
                    #assess the allowed subjects to be enrolled  based from prerequisites
                    $grades = $student->get_evaluation($status_payment['yr_lvl'],$status_payment['semester']);
                    if($status_payment['count']==true){
                             #display the registratio form
                              registar_forms::show_evaluation_form($grades,$_POST['student_id'],$status_payment['yr_lvl'],$status_payment['semester']);  
                              unset($student);    
                    }else{
                            die("<p class='warnings'>You have not yet paid your evaluation fee assessment.</p>");   
                         }
                    #show grades in form
                    
            }else{
                die("<p class='warnings'>you are not a valid user</p>");
            }
               
                                                                                             
                break;
                
            case 'Print Registration Form':
            ####################
             security::record_logs($_SESSION['username'],"Print Registration Form","A POst Method"," Printing Evaluation of : ".$_POST['student_id']);
            #################
                 #get subjects from $_POST and insert to enroll_template table
                 $subjects = admin_functions::get_subjects_form($_POST);
                 
                 #update the student table for the year taken and subjects
                 admin_functions::check_column_taken($_POST,$subjects); 
                 
                 #update the collectibles table
                 admin_functions::insert_to_collectibles($_POST);                      
                 //admin_functions::check_column_taken($_POST['student_id'],$_POST['yr_lvl'],$_POST['semester'],$subjects); 
                      
                break;
               
              case 'View Grade':
                  #this function shows the grade of student per semester
                  $student=new general_student($_SESSION['profile_id']);
                    if($student->check_valid_user()){
                        $grades  = $student->get_cog($_POST['yr_lvl'],$_POST['semester']);
                        student_forms::view_grade($grades,$_POST['yr_lvl'],$_POST['semester']);
                    }else{
                        echo "you are not a valid user";
                        }
                unset($student);
                break;
     
                case 'Apply Settings': 
                    #this function will override default settings for syear
                        admin_functions::setting_configure($_POST); 
                    break;  
    
                    
                case 'Enter Grades':
                #THIS IS FOR dean
                ###############
                  security::record_logs($_SESSION['username'],"Enter Grades","Enters grades and Updates","Entering Grades of student Id: ".$_POST['student_id']);
                #################
                    $data = admin_functions::enter_dean_grades($_POST);
                    dean_forms::grade_update_form($data,$_POST);
                   break; 
                   
                 case 'Enter Grade':
                 #this is for teachers
                    teacher_actions::update_grade($_POST);
                    echo "You have successfully updated your record.";
                    break;  
                
                case 'Evaluate Subjects':
                    admin_functions::enter_dean_grades($_POST);
                    break;
                    
                case 'Update Student Grade':
                ###############
                 security::record_logs($_SESSION['username'],"Update Student Grade","Updates the grade of students","Updating Grades of Student ID: ".$_POST['student_id']);
                #############
                      admin_functions::update_student_grade($_POST);  
                    break;   
                
                case 'Section':
                ####################
                security::record_logs($_SESSION['username'],"Section","Sections the student","Sectioning Student Id: ".$_POST['student_id']);
                ##################
                    admin_functions::section_him_her($_POST,$action);
                    break; 
                                            
                case 'Add Student':
                ####################
                security::record_logs($_SESSION['username'],"Add Student","Adding a new student","Adding new student with Id no: ".$_POST['student_id']);
                ##################
                    admin_functions::add_student($_POST);
                    break;
                    
                case 'ADD':
                case 'DROP':
                    if($_POST['submit']=="ADD"){
                        $student = new general_student($_POST['student_id']);
                        $subjects = $student->show_what_to_add();
                        registar_forms::select_add_subjects($subjects,$_POST);
                       // $data = admin_functions::show_what_to_add($_POST);
                        //registar_forms::override_subject_preq($data);
                    }else{
                        $student = new general_student($_POST['student_id']);
                        $subjects = $student->assess_drop_subjects($_POST['yr_lvl'],$_POST['semester']);
                        registar_forms::select_drop_subjects($subjects,$_POST);
                    }
                    
                    break;
                 case 'DROP SUBJECTS':
                        admin_functions::drop_subject($_POST);
                    break;     
                    
                case 'Add Teacher':
                        admin_functions::add_teacher($_POST);
                        echo "Successfully created a teacher record.";
                        ##########
                        security::record_logs($_SESSION['username'],"Add Teacher","Create a Teacher Ledger","Successfully Created a Teacher w/ Id no: ".$_POST['teacher_id']);
                        #########
                    break;
                    
                case 'Get Students':
                    $students = teacher_actions::get_my_students($_POST);
                    teacher_forms::show_students($students,$_POST);
                    break;  
                    
                case 'Confirm Students List':
                     teacher_actions::insert_to_records($_POST);
                     teacher_actions::update_student_instructor($_POST);
                     echo "You have successfully added students in your records";
                    break;   
                    
                 case 'Update User':
                    #add a user
                    admin_functions::add_edit_delete_users($_POST,"edit");
                break;      
                                                                               
    }           
}
if(isset($_REQUEST['action']) && isset($_SESSION['username'])){
    switch($_REQUEST['action']){
          case 'logout':
           // admin_functions::log_out($_SESSION['username'],$_SESSION['password']);
           admin_functions::log_out($_SESSION['username'],$_SESSION['password']);
            session_destroy();
            header("location:main.php");
            break;
            
           case 'settings':
             #this will enable default settings to be changed
               $syear = miscellaneous::school_year();
               admin_forms::settings_config($syear);
                break;
           
           case 'ask':
               if($_REQUEST['page']=="registrar"){
                    if($_REQUEST['spec']=='cog'){
                        registar_forms::entry_form_cert_grades(); 
                    }elseif($_REQUEST['spec']=='cogreq'){
                        registar_forms::request_cog_lang();
                    }elseif($_REQUEST['spec']=='eval'){
                        registar_forms::entry_form_evaluation();
                    }elseif($_REQUEST['spec']=='evalreq'){
                        registar_forms::request_cog_lang("evaluation");
                    }elseif($_REQUEST['spec']=='enroll'){
                         registar_forms::entry_form();
                    }elseif($_REQUEST['spec']=='view'){
                         #get the students names
                         $names = admin_functions::get_erolled_names();
                         registar_forms::view_enrolled_students($names);
                    }elseif($_REQUEST['spec']=='addsubjects'){
                        registar_forms::add_drop_subjects();
                    }elseif($_REQUEST['spec']=="viewall"){
                        #get all the students name
                        $names = admin_functions::get_all_names_registrar($_REQUEST['start']);
                        #show to the form
                        registar_forms::view_all_students($names);
                    }elseif($_REQUEST['spec']=="addstudent"){
                         $courses = admin_functions::get_courses_option();
                         dean_forms::add_new_student($courses);
                    }elseif($_REQUEST['spec']=="grade"){
                          dean_forms::grade_entry();
                    }                  
               }elseif($_REQUEST['page']=="admin"){
                   if($_REQUEST['spec']=="add"){
                       #get all the users for display
                       $user_type=admin_functions::get_users();
                       #display the adding form
                       admin_forms::add_user($user_type);
                   }elseif($_REQUEST['spec']=="view"){
                        #get all the data to view
                       $data=admin_functions::get_users_profile();
                        admin_forms::view($data);
                   }elseif($_REQUEST['spec']=="delete"){
                       #this will delete the user from user_profile
                       $data=array('username'=>$_REQUEST['username']);
                       admin_functions::add_edit_delete_users($data,"delete");
                   }else{
                        #this will edit the user from user_profile
                       #get all the users for display
                       $user_type=admin_functions::get_users();
                       #get records
                       $records=admin_functions::get_specific_profile($_SESSION['username']);
                       #display the adding form
                       admin_forms::edit_user($user_type,$records);
                   }
            }
               break;
               
          case 'config':
                 if($_REQUEST['page']=="registrar"){
                     
                 }elseif($_REQUEST['page']=="admin"){
                        if($_REQUEST['spec']=="assessment"){
                               #get the assessemtn for option
                               $assessment=admin_functions::get_assessment();
                               #show to forms
                               admin_forms::assessment_names($assessment); 
                        }
                 }
            break;
                     
    }  

}
 ###############################################################################       
cls_General_Forms::code_block_ends();
?>